﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace QLTV_GiaoDien
{
    public partial class frm_Lop : DevExpress.XtraEditors.XtraForm
    {
        public frm_Lop()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaLop;

        public int MaLop
        {
            get { return _MaLop; }
            set { _MaLop = value; }
        }
        private string _TenLop;

        public string TenLop
        {
            get { return _TenLop; }
            set { _TenLop = value; }
        }
        private string _TenDonVi;

        public string TenDonVi
        {
            get { return _TenDonVi; }
            set { _TenDonVi = value; }
        }
        private string _KhoaHoc;

        public string KhoaHoc
        {
            get { return _KhoaHoc; }
            set { _KhoaHoc = value; }
        }
        private string _GhiChu;

        public string GhiChu
        {
            get { return _GhiChu; }
            set { _GhiChu = value; }
        }
        public void EnableTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled =false;
            }
            foreach (Control i in ctr.Controls)
                EnableTextFalse(i);
        }
        public void EnableTextTrue(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled = true;
            }
            foreach (Control i in ctr.Controls)
                EnableTextTrue(i);
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void GetData()
        {
            txt_MaLop.Text = MaLop.ToString();
            txt_TenLop.Text = TenLop;
            cob_TenDonVi.Text = TenDonVi;
            txt_KhoaHoc.Text = KhoaHoc;
            txt_GhiChu.Text = GhiChu;
            //lay du lieu cho combobox
            DataTable dt = new DataTable();
            string sql = "select * from tbl_Lop,tbl_DonVi where tbl_Lop.MaDonVi=tbl_DonVi.MaDonVi and tbl_Lop.MaLop='"+txt_MaLop.Text+"'";
            dt = lop_bll.taoBang(sql);
            cob_MaDonVi.DataSource = dt;
            cob_MaDonVi.DisplayMember = "MaDonVi";
            cob_TenDonVi.DataSource = dt;
            cob_TenDonVi.DisplayMember = "TenDonVi";
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetDaTa;
        public void GetMA(int ma)
        {
            cob_MaDonVi.Text = ma.ToString();
        }
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Danh Mục Lớp";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Danh Mục Lớp";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem Danh Mục Lớp";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                bbi_Luu.Enabled = false;
                GetData();
                EnableTextFalse(this);
            }
        }
        Lop_BLL lop_bll = new Lop_BLL();
        private void frm_Lop_Load(object sender, EventArgs e)
        {
            LoadForm();
            cob_MaDonVi.Hide();
            bbi_Dau.Enabled = false;
            bbi_Sau.Enabled = false;
            bbi_Tiep.Enabled = false;
            bbi_Cuoi.Enabled = false;
            
        }
        private void btn_DonVi_Click(object sender, EventArgs e)
        {
            frm_Lop_DonVi f = new frm_Lop_DonVi();
            f.GetMa = new frm_Lop_DonVi.GetInt(GetMA);
            f.ShowDialog();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();  
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            Lop_Public l = new Lop_Public();
            if (Flag == 0)
            {
                //them
                string d = DateTime.Now.ToString();
                l.TenLop = txt_TenLop.Text;
                l.MaDonVi =int.Parse( cob_MaDonVi.Text);
                l.KhoaHoc = txt_KhoaHoc.Text;
                l.GhiChu = txt_GhiChu.Text;
                lop_bll.Insert_Lop(l,l,l,l);
                //lưu vào nhật ký
                nk.MaNV = tg.MaNV;
                nk.NgayGio =d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm mới Lớp:'" + l.TenLop + "'";
                nhatKy_bll.insert_nhatKy(nk,nk,nk,nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }
            if (Flag == 1)
            {
                //sua
                
                l.MaLop =int.Parse(txt_MaLop.Text);
                l.TenLop = txt_TenLop.Text;
                l.MaDonVi = int.Parse(cob_MaDonVi.Text);
                l.KhoaHoc = txt_KhoaHoc.Text;
                l.GhiChu = txt_GhiChu.Text;
                lop_bll.Update_Lop(l,l, l, l, l);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin của lớp:'"+TenLop.ToString()+"' thành:'" + l.TenLop + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (Flag == 2)
            {
                //xem
                
                l.MaLop = int.Parse(txt_MaLop.Text);
                l.TenLop = txt_TenLop.Text;
                l.MaDonVi = int.Parse(cob_MaDonVi.Text);
                l.KhoaHoc = txt_KhoaHoc.Text;
                l.GhiChu = txt_GhiChu.Text;
                lop_bll.Update_Lop(l, l, l, l, l);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin của lớp:'"+TenLop.ToString()+"' thành:'" + l.TenLop + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            MyGetDaTa(1);
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);

            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            EnableTextTrue(this);
            txt_MaLop.Enabled = false;
            
            Flag = 4;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();

        }

        private void cob_TenDonVi_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_DonVi";
            dt = lop_bll.taoBang(sql);
            cob_MaDonVi.DataSource = dt;
            cob_MaDonVi.DisplayMember = "MaDonVi";
            cob_TenDonVi.DataSource = dt;
            cob_TenDonVi.DisplayMember = "TenDonVi";
        }

        private void cob_MaDonVi_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select * from tbl_DonVi where MaDonVi='" + cob_MaDonVi.Text + "'";
                dt = lop_bll.taoBang(sql);
                cob_MaDonVi.DataSource = dt;
                cob_MaDonVi.DisplayMember = "MaDonVi";
                cob_TenDonVi.DataSource = dt;
                cob_TenDonVi.DisplayMember = "TenDonVi";
            }
            catch (Exception)
            { }
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            this.Text = "Thêm Mới Lớp";
            txt_MaLop.Text = "Mã tự sinh";
            txt_MaLop.Enabled = false;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            this.Text = "Sửa Lớp";
            txt_MaLop.Enabled = true;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }

        private void txt_MaLop_Leave(object sender, EventArgs e)
        {
            if (txt_MaLop.Text == "")
                txt_MaLop.Focus();
        }

        private void txt_MaLop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }


    }
}